package com.netty4.http.xml;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

import java.util.List;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpUtil.setContentLength;

public class HttpXmlResponseEncoder extends AbstractHttpXmlEncoder<HttpXmlResponse> {

    protected void encode(ChannelHandlerContext channelHandlerContext, HttpXmlResponse httpXmlResponse, List<Object> list) throws Exception {
        ByteBuf body = encode0(channelHandlerContext,httpXmlResponse.getResult());
        FullHttpResponse response = httpXmlResponse.getFullHttpResponse();
        if (response == null) {
                response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,body);
        }else {
                response  = new DefaultFullHttpResponse(httpXmlResponse.getFullHttpResponse().protocolVersion(),httpXmlResponse.getFullHttpResponse().status(),body);
        }
        response.headers().set(CONTENT_TYPE,"text/html");
        setContentLength(response,body.readableBytes());
        list.add(response);
    }
}
